package com.jpyl.jpMedicalTreatment.view.widget.sortlistview;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.icu.text.UnicodeSetSpanner;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.util.StringUtil;


/**
 * 购买弹框
 * Created by 15697 on 2019/3/9.
 */

public class MyBuyPopwindow extends PopupWindow implements View.OnClickListener {

    Activity activity;
    String name, price, address;

    TextView tvName, tvPrice;
    EditText et_count, et_address;
    Button btn_sub, btn_add, btn_submit;

    public MyBuyPopwindow(Activity activity, String name, String price, String address) {
        this.activity = activity;
        this.name = name;
        this.price = price;
        this.address = address;
        View view = View.inflate(activity, R.layout.activity_buy_popwindow, null);
        tvName = view.findViewById(R.id.tv_buyPop_name);
        tvPrice = view.findViewById(R.id.tv_buyPop_price);
        et_count = view.findViewById(R.id.et_buyPop_count);
        et_address = view.findViewById(R.id.et_tv_buyPop_address);
        btn_sub = view.findViewById(R.id.btn_buyPop_sub);
        btn_add = view.findViewById(R.id.btn_buyPop_add);
        btn_submit = view.findViewById(R.id.btn_buyPop_submit);
        btn_sub.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        btn_submit.setOnClickListener(this);

        tvName.setText(name);
        tvPrice.setText("单价：" + price + "元");
        et_address.setText(address);
        //菜单背景色
        ColorDrawable dw = new ColorDrawable(0xffffffff);
        setBackgroundDrawable(dw);

        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setOutsideTouchable(true);
        setContentView(view);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_buyPop_sub:
                if (!StringUtil.isEmpty(et_count.getText().toString())) {
                    int count = Integer.parseInt(et_count.getText().toString());
                    if (count > 0) {
                        et_count.setText("" + (--count));
                    }

                }
                break;
            case R.id.btn_buyPop_add:
                if (!StringUtil.isEmpty(et_count.getText().toString())) {
                    int count = Integer.parseInt(et_count.getText().toString());
                    et_count.setText("" + (++count));
                } else {
                    et_count.setText("1");
                }
                break;
            case R.id.btn_buyPop_submit:
                if (StringUtil.isEmpty(et_count.getText().toString())) {
                    Toast.makeText(activity, "请选择数量", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("0".equals(et_count.getText().toString())) {
                    Toast.makeText(activity, "数量不能为0，请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (StringUtil.isEmpty(et_address.getText().toString())) {
                    Toast.makeText(activity, "请输入收货地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (listener != null) {
                    listener.btnSubmit(et_count.getText().toString(), et_address.getText()
                            .toString());
                }

                break;
        }
    }

    public interface MyBuyPopwindowListener {
        void btnSubmit(String count,String address);
    }

    public void setCallBack(MyBuyPopwindowListener listener) {
        this.listener = listener;
    }

    private MyBuyPopwindowListener listener;
}
